package com.sapdroid.sapdroiddo.content;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class KeepDroidContentProvider extends ContentProvider {
	// used for the UriMatcher
	private static final int TODOLIST = 10;
	private static final int TODOLIST_ID = 11;
	private static final int ITEM = 20;
	private static final int ITEM_BY_LIST = 22;

	private static final String AUTHORITY = "com.sapdroid.android.keepdroid.contentprovider";

	private static final String BASE_PATH_LIST = "lists";
	private static final String BASE_PATH_ITEM = "items";
	public static final Uri CONTENT_URI_LIST = Uri
			.parse("content://" + AUTHORITY + "/" + BASE_PATH_LIST);
	public static final Uri CONTENT_URI_ITEM = Uri
			.parse("content://" + AUTHORITY + "/" + BASE_PATH_ITEM);

	private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		sURIMatcher.addURI(AUTHORITY, BASE_PATH_LIST, TODOLIST);
		sURIMatcher.addURI(AUTHORITY, BASE_PATH_LIST + "/#", TODOLIST_ID);
		sURIMatcher.addURI(AUTHORITY, BASE_PATH_ITEM, ITEM);
		sURIMatcher.addURI(AUTHORITY, BASE_PATH_ITEM + "/#", ITEM_BY_LIST);
	}

	private DatabaseHelper database;

	@Override
	public boolean onCreate() {
		database = new DatabaseHelper(getContext());
		return false;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri _uri = null;
		SQLiteDatabase db = database.getWritableDatabase();
		switch (sURIMatcher.match(uri)) {
		case TODOLIST:
			long _ID1 = db.insert(TodoListTable.TABLE_TODOLIST, null, values);
			// if added successfully
			if (_ID1 > 0) {
				_uri = ContentUris.withAppendedId(CONTENT_URI_LIST, _ID1);
				getContext().getContentResolver().notifyChange(_uri, null);
			}
			break;
		case ITEM:
			long _ID2 = db.insert(ItemTable.TABLE_ITEM, null, values);
			// if added successfully
			if (_ID2 > 0) {
				_uri = ContentUris.withAppendedId(CONTENT_URI_ITEM, _ID2);
				getContext().getContentResolver().notifyChange(_uri, null);
			}
			break;
		default:
			throw new IllegalArgumentException("Failed to insert row into " + uri);
		}
		return _uri;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

		int uriType = sURIMatcher.match(uri);
		switch (uriType) {
		case TODOLIST:
			// Set the table
			queryBuilder.setTables(TodoListTable.TABLE_TODOLIST);
			break;
		case TODOLIST_ID:
			// Set the table
			queryBuilder.setTables(TodoListTable.TABLE_TODOLIST);
			// adding the ID to the original query
			queryBuilder.appendWhere(TodoListTable.COLUMN_ID + "=" + uri
					.getLastPathSegment());
			break;
		case ITEM:
			// Set the table
			queryBuilder.setTables(ItemTable.TABLE_ITEM);
			break;
		case ITEM_BY_LIST:
			// Set the table
			queryBuilder.setTables(ItemTable.TABLE_ITEM);
			// adding the ID to the original query
			queryBuilder.appendWhere(ItemTable.COLUMN_LISTID + "=" + uri
					.getLastPathSegment());
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}

		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cursor = queryBuilder
				.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		// make sure that potential listeners are getting notified
		cursor.setNotificationUri(getContext().getContentResolver(), uri);

		return cursor;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = database.getWritableDatabase();
		int delRows = 0;
		switch (sURIMatcher.match(uri)) {
		case ITEM:
			delRows = db.delete(ItemTable.TABLE_ITEM, selection, selectionArgs);
			break;

		case TODOLIST:
			delRows = db.delete(TodoListTable.TABLE_TODOLIST, selection, selectionArgs);
			break;
		}
		
		return delRows;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int updatedRows = 0;
		SQLiteDatabase db = database.getWritableDatabase();
		
		switch (sURIMatcher.match(uri)) {
		case ITEM:
			updatedRows = db.update(ItemTable.TABLE_ITEM, values, selection, selectionArgs);
			break;

		case TODOLIST:
			updatedRows = db.update(TodoListTable.TABLE_TODOLIST, values, selection, selectionArgs);
			break;
		}
				
		return updatedRows;
	}
}
